#include "my_inc/main_login.h"
// 构建函数
static face_p createNode()
{
    static int a = 1;
    face_p node  = malloc(sizeof(face_t));
    memset(node->name, 0, sizeof(node->name));
    if(!node) return NULL; // 检查节点分配是否成功
    if(a) {
        node->screen = lv_scr_act();
        a            = 0;
        lv_obj_set_size(node->screen, 800, 480);
    } else {
        node->screen = lv_obj_create(NULL); // 创建新屏幕（无父对象）
        lv_obj_set_size(node->screen, 800, 480);
    }
    return node;
}

// 屏幕添加到链表
face_p faceinsertend(face_p head, char name[20])
{
    face_p temp = createNode();
    strcpy(temp->name, name);
    list_add_tail(&temp->list, &head->list);
    return temp;
}

// 查找函数
face_p find(face_p head, char name[20])
{
    if(!head) {
        printf("无效的链表头指针!\n");
        return NULL;
    }
    if(list_empty(&head->list)) {
        printf("当前没有界面数据!\n");
        return NULL;
    }
    face_p pos;
    list_for_each_entry(pos, &head->list, list)
    {
        if(!strcmp(pos->name, name)) return pos;
    }
    return NULL;
}

//--------------------------------用户账户链表-----------------------------------------
// 构建函数
damin_p createNode_r()
{
    damin_p node = malloc(sizeof(damin_t));
    if(!node) return NULL; // 检查节点分配是否成功
    memset(node, 0, sizeof(damin_t));
    return node;
}

// 查询账号(name)是否存在(存在返回0，否则返回1)
static bool judge(damin_p head_in, char * name)
{
    if(list_empty(&head_in->list) == 1) // 如果链表为空
        return 1;
    damin_p pos;
    list_for_each_entry(pos, &head_in->list, list)
    {
        if(!strcmp(pos->name, name)) // 说明账号存在
        {
            return 0;
        }
    }
    return 1;
}

// 添加账号
int insertend_reg(damin_p head_in, login_p data_log)
{
    damin_p temp = createNode_r();
    // 验证逻辑
    if(strlen(data_log->username) == 0) {
        lv_label_set_text(label_hint, "用户名不能为空!");
        lv_obj_set_style_text_color(label_hint, lv_color_hex(0xff0000), LV_PART_MAIN);
        start_shake_animation(label_hint);
        return 0;
    }
    if(strlen(data_log->psw) == 0) {
        lv_label_set_text(label_hint, "密码不能为空!");
        lv_obj_set_style_text_color(label_hint, lv_color_hex(0xff0000), LV_PART_MAIN);
        start_shake_animation(label_hint);
        return 0;
    }
    if(temp) // 说明成功分配了内存
    {
        if(judge(head_in, data_log->username)) {
            if(is_valid_password(data_log->psw)) {
                strcpy(temp->name, data_log->username);
                strcpy(temp->psw, data_log->psw);
                list_add_tail(&temp->list, &head_in->list);
                FILE * fp = fopen("user.txt", "ab");
                fwrite(temp, sizeof(damin_t), 1, fp);
                fclose(fp);
                lv_label_set_text(label_hint, "注册成功!");
                lv_obj_set_style_text_color(label_hint, lv_color_hex(0x00ff00), LV_PART_MAIN);
                return 1;
            } else {
                lv_label_set_text(label_hint, "密码至少包含1个大小写字母,和1个数字!");
                lv_obj_set_style_text_color(label_hint, lv_color_hex(0xffff00), LV_PART_MAIN);
                start_shake_animation(label_hint);
                return 0;
            }
        }
        lv_label_set_text(label_hint, "该账户已存在!请重新输入");
        lv_obj_set_style_text_color(label_hint, lv_color_hex(0xff0000), LV_PART_MAIN);
        start_shake_animation(label_hint);
        return 0;
    }
    printf("分配内存失败!\n");
    return -1;
}

// 对比账号密码(都正确返回1，否则返回0,无账号返回-1)
int contrast_reg(damin_p head_in, login_p data)
{
    // 参数检查
    if(!head_in || !data->username) {
        printf("无效输入！\n");
        return -1;
    }
    if(list_empty(&head_in->list)) // 如果链表为空
    {
        lv_label_set_text(label_hint, "暂无账号!请注册！");
        lv_obj_set_style_text_color(label_hint, lv_color_hex(0xff0000), LV_PART_MAIN);
        start_shake_animation(label_hint);
        return -1;
    }
    damin_p temp;
    list_for_each_entry(temp, &head_in->list, list)
    {
        if(temp->name && !strcmp(temp->name, data->username) && !strcmp(temp->psw, data->psw)) {
            lv_label_set_text(label_hint, "登录成功!");
            lv_obj_set_style_text_color(label_hint, lv_color_hex(0x00ff00), LV_PART_MAIN);
            return 1;
        }
    }
    lv_label_set_text(label_hint, "账号或密码错误!请重新输入");
    lv_obj_set_style_text_color(label_hint, lv_color_hex(0xff0000), LV_PART_MAIN);
    start_shake_animation(label_hint);
    return 0;
}
